{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "223d3ae8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "971b946f",
   "metadata": {},
   "source": [
    "1. 数组`a = np.random.rand(3,2,3)`能和`b = np.random.rand(3,2,2)`进行运算吗？能和`c = np.random.rand(3,1,1)`进行运算吗？请说明结果的原因。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4251fa45",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (3,2,3) (3,2,2) ",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_15168/968480992.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrand\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrand\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (3,2,3) (3,2,2) "
     ]
    }
   ],
   "source": [
    "a = np.random.rand(3,2,3)\n",
    "b = np.random.rand(3,2,2)\n",
    "a + b\n",
    "# 答案：a和b不能参与运算，因为不满足广播的机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f5655ff8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0.52861786 0.88262433 0.49510378]\n",
      "  [0.29364156 0.01556228 0.84110193]]\n",
      "\n",
      " [[0.49489336 0.59547142 0.01519171]\n",
      "  [0.15537244 0.46482167 0.34892366]]\n",
      "\n",
      " [[0.14176147 0.58978936 0.33785688]\n",
      "  [0.17597699 0.23812355 0.92497561]]]\n",
      "[[[0.56599239]]\n",
      "\n",
      " [[0.61434546]]\n",
      "\n",
      " [[0.36079343]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[[1.09461025, 1.44861673, 1.06109618],\n",
       "        [0.85963395, 0.58155467, 1.40709433]],\n",
       "\n",
       "       [[1.10923882, 1.20981688, 0.62953717],\n",
       "        [0.7697179 , 1.07916713, 0.96326912]],\n",
       "\n",
       "       [[0.5025549 , 0.95058279, 0.69865031],\n",
       "        [0.53677041, 0.59891698, 1.28576904]]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.rand(3,2,3)\n",
    "c = np.random.rand(3,1,1)\n",
    "print(a)\n",
    "print(c)\n",
    "a + c\n",
    "# 答案：a和c可以参与运算，因为满足广播机制（a和b的形状值虽然不相等，但是不相等的地方都是等于1）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d261952",
   "metadata": {},
   "source": [
    "2. 想要将数组`a = np.arange(15).reshape(3,5)`和`b = np.arange(100,124).reshape(6,4)`叠加在一起，其中`a`在`b`的上面，并且在`b`的第1列后面（下标从0开始）新增一列，用0来填充。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "af42f1da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0.,   1.,   2.,   3.,   4.],\n",
       "       [  5.,   6.,   7.,   8.,   9.],\n",
       "       [ 10.,  11.,  12.,  13.,  14.],\n",
       "       [100., 101.,   0., 102., 103.],\n",
       "       [104., 105.,   0., 106., 107.],\n",
       "       [108., 109.,   0., 110., 111.],\n",
       "       [112., 113.,   0., 114., 115.],\n",
       "       [116., 117.,   0., 118., 119.],\n",
       "       [120., 121.,   0., 122., 123.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二题答案：\n",
    "# 1.准备好数据\n",
    "a = np.arange(15).reshape(3,5)\n",
    "b = np.arange(100,124).reshape(6,4)\n",
    "# 2.因为b只有4列，无法直接和a堆叠，并且题目要求要在第一列后面添加一列\n",
    "# 所以先将b数组在下标为1的地方切割，然后添加完0数组后再进行拼接\n",
    "bsplits = np.hsplit(b,[2])\n",
    "# 3.创建一个全0的6行1列的数组\n",
    "bzero = np.zeros((6,1))\n",
    "# 4.将b的前半部分，0，b的后半部分组合在一起形成一个新的数组\n",
    "c = np.hstack([bsplits[0],bzero,bsplits[1]])\n",
    "# 5.将a和新生成的数组进行堆叠得到结果\n",
    "result = np.vstack([a,c])\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "857e6114",
   "metadata": {},
   "source": [
    "3. 将数组`a = np.random.rand(4,5)`扁平化成一维数组，可以使用`flatten`和`ravel`，对两者的返回值进行操作，哪个会影响到数组`a`？对会影响到`a`数组的那个函数，请说明原因。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2c0de859",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.35230824 0.80393808 0.28507039 0.08024001 0.31404108]\n",
      " [0.75122143 0.97510412 0.48397677 0.90645693 0.17172821]\n",
      " [0.98978892 0.33049243 0.19139348 0.25470182 0.42878057]\n",
      " [0.70483148 0.78936745 0.59339431 0.10099746 0.63417672]]\n",
      "==============================\n",
      "[[20.          0.80393808  0.28507039  0.08024001  0.31404108]\n",
      " [ 0.75122143  0.97510412  0.48397677  0.90645693  0.17172821]\n",
      " [ 0.98978892  0.33049243  0.19139348  0.25470182  0.42878057]\n",
      " [ 0.70483148  0.78936745  0.59339431  0.10099746  0.63417672]]\n"
     ]
    }
   ],
   "source": [
    "# 第三题答案：\n",
    "a = np.random.rand(4,5)\n",
    "a1 = a.flatten()\n",
    "a1[0] = 10\n",
    "print(a)\n",
    "print('='*30)\n",
    "a2 = a.ravel()\n",
    "a2[0] = 20\n",
    "print(a)\n",
    "# 结果：ravel会影响原来的数组。原因是因为ravel返回的是一个浅拷贝（视图），虽然在栈中的内存不一样，但是指向的堆区的内存地址还是一样\n",
    "# 所以修改了a2,会影响到原来堆区的值。但是flatten返回的是一个深拷贝，也即栈区和堆区都进行了拷贝。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de69c09c",
   "metadata": {},
   "source": [
    "4.使用numpy自带的csv方法读取出stock.csv文件中preClosePrice、openPrice、highestPrice、lowestPrice的数据（提示：使用skiprows和usecols参数）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bd2aa462",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\winlab\\AppData\\Local\\Temp/ipykernel_15168/3236746195.py:1: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.\n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  stocks = np.loadtxt(\"stock.csv\",dtype=np.float,delimiter=\",\",skiprows=1,usecols=[6,7,8,9])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[13.38 , 13.4  , 13.48 , 12.96 ],\n",
       "       [31.22 , 30.5  , 32.03 , 30.5  ],\n",
       "       [25.56 , 25.41 , 26.4  , 25.18 ],\n",
       "       ...,\n",
       "       [ 0.507,  0.507,  0.508,  0.507],\n",
       "       [ 1.675,  1.67 ,  1.69 ,  1.67 ],\n",
       "       [ 0.976,  0.976,  0.988,  0.976]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks = np.loadtxt(\"stock.csv\",dtype=np.float,delimiter=\",\",skiprows=1,usecols=[6,7,8,9])\n",
    "stocks"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
